﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.DocManger;

using Jzy.Model.DocManger;
using Frame.Lib.Utilities;
using Frame.Lib.Common;

namespace Jzy.Web.DocManager.Dir
{
    public partial class MoveDlg : PageSelectTree
    {
        private DirBO Dir
        {
            get
            {
                return DirBO.Instance<DirBO>();
            }
        }

        private bool IsPrivate
        {
            get
            {
                if (string.IsNullOrEmpty(Request["IsPrivate"]))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Title"] != null)
                {
                    lblTitle.Text = Request["Title"];
                }
                BindTree();
            }
            tvList.Attributes.Add("onclick", "SingleSelect()");
        }

        private void BindTree()
        {
            List<DirInfo> list = this.Dir.GetList();
            
            if (IsPrivate)
            {
                list = list.Where(T => T.UpperID == 0 || (T.CreatorID == AppSession.UserID && T.IsPrivate == true)).ToList();
            }
            else
            {
                list = list.Where(T => T.IsPrivate == false).ToList();
            }

            this.Dir.BindTreeView(tvList, list, true, 0, null);


            tvList.ExpandAll();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strSelectIDs = Request["SelectIDs"];
            List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);

            List<int> lstDirIDs = StrUtil.SplitToInt(CheckedIDs);
            if (lstDirIDs.Count > 0)
            {
                int destIDs = lstDirIDs.First();
                if (Dir.Include(lstSelectIDs, destIDs))
                {
                    JSUtil.Alert("目标目录不得包括自己及子目录");
                    BindTree();
                    return;
                }
                Dir.Move(lstSelectIDs, destIDs);
            }
            JSUtil.ResponseJS("close();returnValue=true");
        }  
    }
}